package cn.madis.ymcc.mq;

import ch.qos.logback.core.util.ContextUtil;
import cn.madis.ymcc.domain.MediaFile;
import cn.madis.ymcc.result.JSONResult;
import cn.madis.ymcc.service.IMediaFileService;
import cn.madis.ymcc.service.impl.MediaFileServiceImpl;
import com.alibaba.fastjson.JSONObject;
import io.netty.util.CharsetUtil;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        consumerGroup = "service-media-producer",//组名
        topic = "topic-media",
        selectorExpression = "tags-media",//tags
        messageModel = MessageModel.CLUSTERING//集群模式一个消费者只能给到一个消费者

)
public class MediaConsumer implements RocketMQListener<MessageExt>{

    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String s = new String(body, CharsetUtil.UTF_8);
        MediaFile mediaFile = JSONObject.parseObject(s, MediaFile.class);
        JSONResult jsonResult = mediaFileService.handleFile2m3u8(mediaFile);
        if (jsonResult.isSuccess()){
            mediaFile.setFileStatus(2);
        }else {
            mediaFile.setFileStatus(3);
        }
        mediaFileService.updateById(mediaFile);

    }
}
